
console.log('this is  myAccount.js');
var _account_service = require('service/account-service.js');

require('page/common/header/index.js');
var myAccount = {
    init: function () {
        this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    //点击事件
    bindEvents: function () {
        var _this = this;
        //检验密码
        $('#newPassword').on('keyup', function () {

            if (this.value.length < 8 && this.value.length > 2) {

                $('#passwordTips').attr("class", 'successMsg').text('密码可用');
            }
            else {

                $('#passwordTips').attr("class", 'errorMsg').text('密码长度大于2不超过7');
            }
        });
        $('#repeatedPassword').on('keyup', function () {

            if (this.value === $('#newPassword').val()) {
                console.log("yes");
                $('#repeatedPasswordTips').attr("class", null).text('');

            }
            else {
                console.log("no");
                $('#repeatedPasswordTips').attr("class", 'errorMsg').text('密码不一致');
            }
        });


        //修改密码


        $('#updateSignOn').on('click', function () {
            if ($('#newPassword').val() === $('#repeatedPassword').val()) {
                var account = {
                    username: $('#username').val(),
                    password: $('#newPassword').val(),
                };
                console.log(account.username);
                console.log(account.password);
                _account_service.updatePassword(
                    account,
                    function (resp) {
                        $('#newPassword', '#repeatedPassword').val('');
                        alert('密码修改成功!');

                    },
                    function (errorMsg) {
                        alert('密码修改失败!');
                    }
                );
            }
            else {
                alert("密码不一致");
            }
        });
        //更新信息
        $('#updateAll').on('click', function () {
            _account_service.update(
                $('#updateAccount').serialize(),
                function (resp) {
                    _this.showMessage(resp);
                    alert('账户修改成功!');

                },
                function (errorMsg) {
                    alert('密码修改失败!');
                }
            )
        });

    },
    loadAccountInfo: function () {
        var _this = this;
        _account_service.checkLogin(
            //成功调用//res 为用户
            function (res) {
                _this.showMessage(res);
                console.log('res:' + res.username);
                // alert("欢迎用户:" + res.username);


            },
            //失败调用
            function (errMsg) {
                console.log('errrrrr');
            }

        );

    },
    showMessage: function (res) {
        $('#username').val(res.username);

        $('#firstName').val(res.firstName);
        $('#lastName').val(res.lastName);
        $('#email').val(res.email);
        $('#phone').val(res.phone);
        $('#address1').val(res.address1);
        $('#address2').val(res.address2);
        $('#city').val(res.city);
        $('#state').val(res.state);
        $('#zip').val(res.zip);
        $('#country').val(res.country);
        $('#languagePreference').val(res.languagePreference);
        $('#favouriteCategoryId').val(res.favouriteCategoryId);
        $('#listOption').attr('checked', res.listOption);
        $('#bannerOption').attr('checked', res.bannerOption);
    }
}
module.exports = myAccount.init();